﻿package com.telaxo.shooter2012.gfx {
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class ScoreHUD extends MovieClip {
		[Embed(source = "swf/Life.swf")]
		private var Ressource : Class;
		private var myScoreTxt : TextField;
		private var myComboTxt : TextField;
		private var myScore : uint;
		private var myCombo : uint;
		private var stageRef : Stage;
		private var myTextFormat : TextFormat;
		private var heartContainer : MovieClip;
		private var heartShowed : int;
		private var heartMax : int;
		private var life : Vector.<MovieClip>;

		public function ScoreHUD(stageRef : Stage) {
			this.stageRef = stageRef;

			myScoreTxt = new TextField();
			myComboTxt = new TextField();

			myTextFormat = new TextFormat("Arial", 13, 0xffffff);

			myComboTxt.defaultTextFormat = myTextFormat;
			myScoreTxt.defaultTextFormat = myTextFormat;

			myScoreTxt.text = "SCORE " + "\n" + myScore;
			myComboTxt.text = "MULTIPLIER " + "\n" + myCombo;

			myScoreTxt.alpha = 0.7;
			myComboTxt.alpha = 0.7;

			myCombo = 1;
			myScore = 0;

			// Nombre de coeur visible
			heartShowed = 0;

			myComboTxt.y = 30;

			// Ajout des vies à l'écrans
			life = new Vector.<MovieClip>();
			heartContainer = new MovieClip();

			this.stageRef.addChild(myScoreTxt);
			this.stageRef.addChild(myComboTxt);
			this.stageRef.addChild(heartContainer);

			x = 50;
			y = stageRef.stageHeight - height - 50;
		}

		public function updateKills(value : Number) : void {
			// s_kills += value;
			// kills.text = String(s_kills);
		}

		public function updateHits(value : Number) : void {
			// s_hits += value;
			// hits.text = String(s_hits);
		}

		public function updateScore(value : Number) : void {
			myScore += value;
			myScoreTxt.text = "SCORE " + "\n" + myScore;
		}

		// Initialise le conteneur de coeurs
		public function initLife(value : Number) : void {
			for (var i : int; i < value; i++) {
				var heart : MovieClip = new Ressource();
				heart.x = stageRef.stageWidth - (heart.width * i + 20);
				heart.y = 12;
				life.push(heart);
				heartContainer.addChild(life[i]);
				heartShowed++;
			}
			heartMax = heartContainer.numChildren;
		}

		// Actualisation des coeurs
		public function updateLife(value : Number) : void {
			// Enléve des coeurs
			if (heartShowed > value) {
				if (heartShowed > 0) {
					for (var i : int = heartShowed; i > value; i--) {
						life[i - 1].visible = false;
						heartShowed--;
					}
				}
			} 
			
			// Rajoute des coeurs 
			if ( heartShowed <= value) {
				if(heartShowed <= heartMax){
					for (var j : int = heartShowed; j < value; j++) {
						trace(j);
						life[j - 1].visible = true;
						heartShowed++;
					}
				}
			}
		}
	}
}

